﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.MO.Enums;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForSOtoMO : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //生产订单
            UFIDA.U9.MO.MO.MO mo = key.GetEntity() as UFIDA.U9.MO.MO.MO;
            if (mo == null) return;

            #region 
            if (mo.SrcDoc.MOSrcDocType == MOSrcDocTypeEnum.SO)
            {
                //引入销售订单
                SO so = SO.Finder.FindByID(mo.SrcDoc.SrcDoc.ID);
                if (so == null) return;

                if (mo.SrcDoc.SrcDocLineNo != null && mo.SrcDoc.SrcDocLineNo != "")
                {
                    decimal DsSo;
                    decimal DsMo;
                    //针对销售订单行查询
                    string sql = $@"SELECT
                                    line.OrderByQtyPU qty
                                    ,subline.SOShipLineSumInfo_TransformQtyTU transQty
                                    FROM SM_SO so		
                                    LEFT JOIN SM_SOLine line ON line.SO = so.ID
                                    LEFT JOIN SM_SOShipline subLine ON subLine.SOLine = line.ID
                                    where so.DocNo  = '{mo.SrcDoc.SrcDocNo}' AND line.DocLineNo  = '{mo.SrcDoc.SrcDocLineNo}'";
                    DataSet ds = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);

                    //销售订单数量
                    if (ds.Tables[0].Rows[0][0] == System.DBNull.Value)
                    {
                        DsSo = 0;
                    }
                    else
                    {
                        DsSo = decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
                    }
                    //已转生产数量
                    if (ds.Tables[0].Rows[0][1] == System.DBNull.Value)
                    {
                        DsMo = 0;
                    }
                    else
                    {
                        DsMo= decimal.Parse(ds.Tables[0].Rows[0][1].ToString());
                    }


                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    
                    /*
                     * 
                    decimal Ds1So;
                    decimal Ds1Mo;
                    //查询销售订单总数量以及已转总数量
                    string sql1 = $@"SELECT
                                    SUM(line.OrderByQtyPU) qty
                                    ,SUM(subline.SOShipLineSumInfo_TransformQtyTU) transQty
                                    FROM SM_SO so		
                                    LEFT JOIN SM_SOLine line ON line.SO = so.ID
                                    LEFT JOIN SM_SOShipline subLine ON subLine.SOLine = line.ID
                                    where so.DocNo  = '{mo.SrcDoc.SrcDocNo}'
                                    GROUP BY so.DocNo";
                    DataSet ds1 = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);

                    //销售订单总数量
                    if (ds1.Tables[0].Rows[0][0] == System.DBNull.Value)
                    {
                        Ds1So=  0;
                    }
                    else
                    {
                        Ds1So = decimal.Parse(ds1.Tables[0].Rows[0][0].ToString());
                    }
                    //已转生产总数量
                    if (ds1.Tables[0].Rows[0][1] == System.DBNull.Value)
                    {
                        Ds1Mo = 0;
                    }
                    else
                    {
                        Ds1Mo = decimal.Parse(ds1.Tables[0].Rows[0][1].ToString());
                    }

                    decimal Ds2Mo;

                    //查询生产订单总生产数量
                    string sql2 = $@"SELECT SUM(ProductQty) productQty  FROM MO_MO 
                                        where SrcDoc_SrcDocNo = '{mo.SrcDoc.SrcDocNo}'
                                        GROUP BY SrcDoc_SrcDocNo";
                    DataSet ds2 = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql2, null, out ds2);
                    //生产订单总生产数量
                    if (ds2.Tables[0].Rows.Count <=0 )
                    {
                        Ds2Mo = 0;
                    }
                    else
                    {
                        Ds2Mo = decimal.Parse(ds2.Tables[0].Rows[0][0].ToString());
                    }
                   */



                    //销售订单数量 - 已转数量 >生产数量
                    if (DsSo - DsMo < mo.ProductQty)
                    {
                        throw new Exception($"该销售订单:'[{mo.SrcDoc.SrcDocNo}]',行号:'[{mo.SrcDoc.SrcDocLineNo}]',生产数量:'[{mo.ProductQty}]' > 累计可转数量:'[{DsSo - DsMo}]'");
                    }


                    /*
                    //销售订单总数量 - 已转总数量 >生产总数量
                    if (Ds1So - Ds1Mo < Ds2Mo)
                    {
                        throw new Exception($"该销售订单:'[{mo.SrcDoc.SrcDocNo}]',生产总数量:'[{Ds2Mo}]' > 累计可转总数量:'[{Ds1So - Ds1Mo}]'");
                    }
                    */



                }


                #endregion

            }

        }
    }
}
